#pragma once
#include "Model(MySQL).hpp"
#include<ctemplate/template.h>

namespace ns_view
{
    using namespace ns_model;
    static const string Template_path = "./template_html/";

    class View
    {
    public:
        string ListHtml(const set<Info> &lst)
        {
            string src_html = Template_path + "QuestionBankList.html";

            //构建字典 KV结构
            ctemplate::TemplateDictionary root("QuestionBankList");//字典识别标识
            for (auto& e : lst)
            {
                //嵌套子字典
                ctemplate::TemplateDictionary* sub = root.AddSectionDictionary("question");
                //掺入数据
                sub->SetValue("id", e._id);
                sub->SetValue("title", e._title);
                sub->SetValue("level", e._level);
            }

            //构建渲染模板对象
            ctemplate::Template * tpe = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            //把字典中的值替换到模板对象中
            string ret;
            tpe->Expand(&ret, &root);
            return ret;
        }

        string QuestionHtml(const Question& qsn)
        {
            string src_html = Template_path + "OneQuestion.html";

            //构建字典
            ctemplate::TemplateDictionary root("question");
            root.SetValue("id", qsn._id);
            root.SetValue("title", qsn._title);
            root.SetValue("level", qsn._level);
            root.SetValue("desc", qsn._desc);
            root.SetValue("show", qsn._show);

            //构建渲染模板对象
            ctemplate::Template* tle = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            //把字典中的值替换到模板对象中
            string ret;
            tle->Expand(&ret, &root);

            return ret;
        }
    };
}